package co.yixiang.modules.adopt.serviceimpl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.FileUtil;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.adopt.entity.domain.YxAdoptFeature;
import co.yixiang.modules.adopt.entity.dto.YxAdoptFeatureQueryCriteria;
import co.yixiang.modules.adopt.mapper.AdoptFeatureMapper;
import co.yixiang.modules.adopt.service.YxAdoptFeatureService;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxAdoptFeatureServiceImpl extends BaseServiceImpl<AdoptFeatureMapper, YxAdoptFeature> implements YxAdoptFeatureService {
    private final IGenerator generator;

    public YxAdoptFeatureServiceImpl(IGenerator generator) {
        this.generator = generator;
    }

    @Override
    public Map<String, Object> queryAll(YxAdoptFeatureQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxAdoptFeature> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YxAdoptFeature> queryAll(YxAdoptFeatureQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxAdoptFeature.class, criteria));
    }

    @Override
    public void download(List<YxAdoptFeature> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxAdoptFeature yxAdoptFeature : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("ID", yxAdoptFeature.getId());
            map.put("特点名称", yxAdoptFeature.getName());
            map.put("描述", yxAdoptFeature.getDesc());
            map.put("排序", yxAdoptFeature.getSort());
            map.put("图标", yxAdoptFeature.getIcon());
            map.put("是否隐藏", yxAdoptFeature.getHidden());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public YxAdoptFeature saveVariety(YxAdoptFeature adoptFeature) {
        // 添加商品
        this.save(adoptFeature);
        return adoptFeature;
    }

    @Override
    public void updateFeature(YxAdoptFeature resources) {
        this.saveOrUpdate(resources);
    }

    @Override
    public void delete(Integer id) {
        this.removeById(id);
    }

    @Override
    public boolean checkFeatureExist(String name, Integer id) {
        return false;
    }

    @Override
    public List<YxAdoptFeature> selectListByCateId(Integer cateId) {
        return null;
    }
}
